package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiArticle;
import com.lk.ii.domain.IiCategory;
import com.lk.ii.domain.IiAuthor;
import com.lk.ii.service.IIiArticleService;
import com.lk.ii.service.IIiCategoryService;
import com.lk.ii.service.IIiAuthorService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author lk
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/ii/article")
public class IiArticleController extends BaseController
{
    private String prefix = "ii/article";

    @Autowired
    private IIiArticleService iiArticleService;

    @Autowired
    private IIiCategoryService iiCategoryService;

    @Autowired
    private IIiAuthorService iiAuthorService;

    @RequiresPermissions("ii:article:view")
    @GetMapping()
    public String article()
    {
        return prefix + "/article";
    }

    /**
     * 查询文章管理列表
     */
    @RequiresPermissions("ii:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiArticle iiArticle)
    {
        startPage();
        List<IiArticle> list = iiArticleService.selectIiArticleList(iiArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @RequiresPermissions("ii:article:export")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiArticle iiArticle)
    {
        List<IiArticle> list = iiArticleService.selectIiArticleList(iiArticle);
        ExcelUtil<IiArticle> util = new ExcelUtil<IiArticle>(IiArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增文章管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //分类列表
        IiCategory iiCategory = new IiCategory();
        iiCategory.setType("news");
        iiCategory.setStatus(1);
        List<IiCategory> categories=iiCategoryService.selectIiCategoryList(iiCategory);
        mmap.put("categories",categories);
        //作者列表
        IiAuthor iiAuthor = new IiAuthor();
        iiAuthor.setStatus(1);
        List<IiAuthor> authors=iiAuthorService.selectIiAuthorList(iiAuthor);
        mmap.put("authors",authors);
        return prefix + "/add";
    }

    /**
     * 新增保存文章管理
     */
    @RequiresPermissions("ii:article:add")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiArticle iiArticle)
    {
        iiArticle.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiArticleService.insertIiArticle(iiArticle));
    }

    /**
     * 修改文章管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //分类列表
        IiCategory iiCategory = new IiCategory();
        iiCategory.setType("news");
        iiCategory.setStatus(1);
        List<IiCategory> categories=iiCategoryService.selectIiCategoryList(iiCategory);
        mmap.put("categories",categories);
        //作者列表
        IiAuthor iiAuthor = new IiAuthor();
        iiAuthor.setStatus(1);
        List<IiAuthor> authors=iiAuthorService.selectIiAuthorList(iiAuthor);
        mmap.put("authors",authors);
        IiArticle iiArticle = iiArticleService.selectIiArticleById(id);
        mmap.put("iiArticle", iiArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章管理
     */
    @RequiresPermissions("ii:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiArticle iiArticle)
    {
        iiArticle.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiArticleService.updateIiArticle(iiArticle));
    }

    /**
     * 删除文章管理
     */
    @RequiresPermissions("ii:article:remove")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiArticleService.deleteIiArticleByIds(ids));
    }
}
